using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// A <see cref="IBusinessObjectRule"/> that validates that a <see cref="UIView"/>
    /// has an <see cref="UIGridInfo"/> and an <see cref="UIFormInfo"/> defined
    /// </summary>
    public class UIViewMustHaveUIGridOrUIForm : IBusinessObjectRule
    {
        private UIView UIView { get; set; }

//        public UIViewMustHaveUIGridOrUIForm(UIView uiView)
//        {
//            if (uiView == null) throw new ArgumentNullException("uiView");
//            if (uiView.ClassID == null && uiView.Class == null)
//            {
//                throw new HabaneroArgumentException("uiView", "The uiView.Class cannot be null for the uiView");
//            }
//            UIView = uiView;
//        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            UIView = (UIView) bo;
            return this.UIView.UIGridInfo != null || this.UIView.UIFormInfo != null;
        }

        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "UIViewMustHaveUIGridOrUIForm"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get
            {
                return "The UIView '" + UIView.ViewName + "' for the Entity/Class '" + UIView.Class
                       + "' must have one UIGrid defined or one UIForm Defined (or both)";
            }
        }

        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Warning; }
        }

        #endregion
    }
}